package GSEA::Chart::ChartCreator ;

use strict ;
use warnings;

sub constrChartCreator($$$$) #constructor
{
	my ($classe , $graphoutPath , my $width , my $height ) = @_; 
	
	if( ! defined($graphoutPath)	){ die("argument 'graphoutPath' is not set , error");}
	if( ! defined($width) 			){ die("argument 'width' is not set , error");}
	if( ! defined($height)  		){ die("argument 'height' is not set , error");}

	
	my $this = {			
					"graphoutPath"	=> $graphoutPath,
					"width"			=> $width,
					"height"		=> $height
					
				};
	
	bless ($this,$classe);  
		
	return $this;  
}



sub writeScript( $$$ )
{
	( my $this ,  my $filename , my @script )= @_ ;
	
	if( ! open(SCRIPT , ">$filename.input") ) { die("huh.. error happened while trying to create '$filename' : $! "); } 

	for(my $i = 0 ; $i< @script ; $i++ )
	{
		my $line = $script[$i] ;
		print(SCRIPT $line);
		print(SCRIPT "\n");
	}
	
	close(SCRIPT);
}


sub drawEScourb($$$)
{
	( my $this , my $filename , my $pt_ES ) = @_ ; 	
	my @ES = @{ $pt_ES };
	
	#CREATE test.png (600, 300)
	#COLOR (0,0,1)
	#TEXTCOLOR (0,0,0)
	#XAXIS 0 TO 20 STEP 2
	#YAXIS 0 TO 1 STEP 0.25
	#COLOR (0.8,0.8,0.8)
	#GRID
	#COLOR (0.2,0.2,0.2)
	#STARTPLOT LINEAR
	#PLOTPOINT (0,0.11)
	
	my @script ;
	
	# header
	my $width  = $this->{"width"};
	my $height = $this->{"height"};
	push(@script , "CREATE ".$filename.".png (".$width.",".$height.")");
	push(@script , "COLOR (0,0,1)");
	
	# creating Axis
	push(@script , "TEXTCOLOR (0,0,0)");
	push(@script , "XAXIS 0 TO ".($#ES  )." STEP 1 ");
	push(@script , "YAXIS -1 TO 1 STEP 0.25");
	
	# setting the grid
	push(@script , "COLOR (0.8,0.8,0.8)");
	push(@script , "GRID");
	
	# pushing values
	push(@script , "COLOR (0.2,0.2,0.2)");
	push(@script , "STARTPLOT LINEAR");
	
	for(my $i =0 ; $i < @ES ; $i++)
	{
		my $value = $ES[$i] ;
		push(@script , "PLOTPOINT (".$i.",".$value.")");
	}
		
	$this->writeScript(  ($filename,@script)  );
}




sub buildClassesFromScores( @ ) 
{

	(my $numberOfClasses , my @scores) = @_ ;

	my @distrib ;
	
	for( my $i = 0 ; $i < $numberOfClasses ; $i++)
	{
		$distrib[$i]= 0 ;
	}
	
	for(my $i = 0 ; $i < @scores  ; $i++ )
	{	
		my $score = $scores[$i];
		
		my $cursor = 0 ;my $ok = 0 ;
		while( $cursor <  ($numberOfClasses) && !$ok )
		{
			my $bot =   -1 +  $cursor    * ( 2 / ($numberOfClasses) ) ;
			my $top =   -1 + ($cursor+1) * ( 2 / ($numberOfClasses) ) ;
			
			if(  ( $bot < $score ) &&  ( $score < $top )  )
			{
				$ok = 1 ;				 
				$distrib[$cursor] ++ ;	
			}			
			$cursor++;
		}
	}
	
	return @distrib ;
}


sub drawScoreDistribution($$$)
{
	( my $this ,my $filename , my $pt_scores ) = @_ ; 	
	my @scores = @{ $pt_scores } ;

	if( length( $filename ) > 25 ){$filename = substr( $filename , 0 , 21)   ;}

	@scores = buildClassesFromScores( 100 , @scores) ;
	
	my $maxHeight = 0 ;
	for(my $i = 0 ; $i < @scores  ; $i++ )
	{
		my $sc = $scores[$i];
		if( $sc > $maxHeight ){ $maxHeight = $sc; }
	}

	#CREATE test.png (600, 300)
	#COLOR (0,0,1)
	#TEXTCOLOR (0,0,0)
	#XAXIS 0 TO 20 STEP 2
	#YAXIS 0 TO 1 STEP 0.25
	#COLOR (0.8,0.8,0.8)
	#GRID
	#COLOR (0.2,0.2,0.2)
	#STARTPLOT HISTOGRAM 1
	#PLOTPOINT (0,0.5)
	#...
	
	my @script ;
	
	# header
	my $width  = $this->{"width"};
	my $height = $this->{"height"};
	push(@script , "CREATE ".$filename.".png (".$width.",".$height.")");
	push(@script , "COLOR (0,0,1)");
	
	# creating Axis
	push(@script , "TEXTCOLOR (0,0,0)");
	push(@script , "XAXIS -1 TO 1 STEP ".( 2 /  ($#scores+1) )." ");
	#push(@script , "XAXIS -1 TO 1 STEP 0.08 ");
	push(@script , "YAXIS 0  TO ".$maxHeight." STEP ".  int( $maxHeight / 5 ) );
	
	# setting the grid
	push(@script , "COLOR (0.8,0.8,0.8)");
	#push(@script , "GRID");
	
	# pushing values
	push(@script , "COLOR (0.2,0.2,0.2)");
	push(@script , "STARTPLOT HISTOGRAM");

	
	for(my $i =0 ; $i < @scores ; $i++)
	{
		my $value = $scores[$i] ;
		my $rank  = ( -1 + ($i * ( 2 /  ($#scores+1)) ) ) ;
				
		$rank = sprintf("%.02f",$rank);		
		push(@script , "PLOTPOINT (". $rank .",".$value.")");
	}
		
	$this->writeScript(  ($filename,@script)  );
	
}

sub drawGeneSetDistribution(@)
{
	( my $this , my $filename , my $pt_distrib ) = @_ ; 	
	my @distrib = @{ $pt_distrib };
		
	my @script ;
	
	# header
	my $width  = $this->{"width"};
	my $height = $this->{"height"};
	push(@script , "CREATE ".$filename.".png (".$width.",".$height.")");
	push(@script , "COLOR (0,0,1)");
	
	# creating Axis
	push(@script , "TEXTCOLOR (0,0,0)");
	push(@script , "XAXIS 0 TO ".($#distrib+1)." STEP 1 ");
	push(@script , "YAXIS 0 TO 1 STEP 0.50");
	
	# setting the grid
	push(@script , "COLOR (0.8,0.8,0.8)");
	push(@script , "GRID");
	
	# pushing values
	push(@script , "COLOR (0.2,0.2,0.2)");
	push(@script , "STARTPLOT HISTOGRAM");
	
	for(my $i =0 ; $i < @distrib ; $i++)
	{
		my $value = $distrib[$i] ;
		my $rank  = $i ;
		
		push(@script , "PLOTPOINT (". $rank .",".$value.")");
	}
	
	$this->writeScript(  ($filename,@script)  );
}



1;